package com.songlanyun.modules.applyareaagent.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.enums.SysConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.modules.applyareaagent.entity.ApplyAreaAgentCheckDTO;
import com.songlanyun.modules.applyareaagent.entity.SysUserForAreaAgentDTO;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.service.SendPrivateMessageMsgService;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.model.vo.ShopSlimVO;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.user.entity.SysUserEntity;
import com.songlanyun.modules.user.entity.SysUserEntityDTO;
import com.songlanyun.modules.user.exception.UserException;
import com.songlanyun.modules.user.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.applyareaagent.entity.ApplyAreaAgentEntity;
import com.songlanyun.modules.applyareaagent.service.ApplyAreaAgentService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;


/**
 * 申请区域代理
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2022-09-29 14:33:03
 */
@RestController
@RequestMapping("admin/applyareaagent")
public class ApplyAreaAgentAdminController extends AbstractController {
    @Autowired
    private ApplyAreaAgentService applyAreaAgentService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SendPrivateMessageMsgService sendPrivateMessageMsgService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    //@RequiresPermissions("applyareaagent:applyareaagent:list")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = applyAreaAgentService.queryCustomPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 申请审核
     */
    @PostMapping("/check")
    //@RequiresPermissions("applyareaagent:applyareaagent:list")
    public R check(@RequestBody ApplyAreaAgentCheckDTO dto) {
        ApplyAreaAgentEntity check = applyAreaAgentService.check(dto);

        //发送系统消息
        sendPrivateMessageMsgService.send(new MessageTO(PrivateMessageConstant.MessageType.SYSTEM_MESSAGES,
                PrivateMessageConstant.ModelType.CARRIER_AUDIT, check.getId(), check.getUserId()));
        return R.ok();
    }

    /**
     * 平台为区域代理分配管理员账号
     */
    @SysLog("平台为区域代理分配管理员账号")
    @PostMapping("/assignedAccount")
    public R assignedAccount(@RequestBody SysUserForAreaAgentDTO user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);
        verifyAdmin();

        //查询该区域代理信息
        ApplyAreaAgentEntity areaAgent = applyAreaAgentService.loadById(user.getAreaAgentId(), true);

        //区域代理已分配管理员账号
        if (ObjectUtil.isNotNull(areaAgent.getAdminId())) {
            throw new RRException(UserException.AREA_AGENT_ASSIGNED_ADMINISTRATOR);
        }


        //校验用户名和手机号
        sysUserService.verifyUserName(user.getUsername(), user.getMobile());

        //分配账号
        user.setCreateUserId(getUserId());//创建人ID
        applyAreaAgentService.assignedAccount(areaAgent, user);

        return R.ok();
    }

    /**
     * 根据区域代理查询管理员主账号
     */
    @SysLog("根据区域代理查询管理员主账号")
    @GetMapping("/getAdmin/{id}")
    public R getShopAdmin(@PathVariable("id") Long id) {

        SysUserEntity user = applyAreaAgentService.getAdminById(id);

        return R.ok().put("data", user);
    }


    /**
     * 删除被拒绝的申请
     */
    @SysLog("删除被拒绝的申请")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        ApplyAreaAgentEntity entity = applyAreaAgentService.loadById(id, true);
        if (entity.getStatus() != 2) {
            throw new RRException("只有被拒绝的代理申请才可以被删除");
        }

        applyAreaAgentService.remove(id);

        return R.ok();
    }


    /**
     * 区域代理范围内的商家
     */
    @RequestMapping("/myshops/list")
    //@RequiresPermissions("offlineorder:offlineorder:list")
    public R myOfflineShops(){
        List<ShopSlimVO> list = applyAreaAgentService.myOfflineShops(getAreaAgentId());
        return R.ok().put("list", list);
    }

}
